package thread;

/**
 * Creared with IntelliJ IDEA.
 * Description:
 * User:yxd
 * Date:2022-03-20
 * Time:23:17
 */
public class Demo6 {
    public static boolean falg = false;
    public static void main(String[] args) {
        Thread t = new Thread(()->{//如何结束一个线程呢 可以设置一个标志位
            while(!falg){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //只要改变标志位就可以停止
        t.start();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束");
        falg = true;
    }
}
